package com.aviator.detective.execute.processor;

import com.aviator.detective.execute.context.ExecuteContext;

import java.util.*;

public class ExecutePostProcessorChain {

    private final Set<AviatorPostProcessor> aviatorPostProcessors = new TreeSet<>(new Comparator<AviatorPostProcessor>() {
        @Override
        public int compare(AviatorPostProcessor o1, AviatorPostProcessor o2) {
            return o1.getOrder() - o2.getOrder();
        }
    });

    public void beforProcessAllByOrder(ExecuteContext context){
        for (AviatorPostProcessor aviatorPostProcessor : aviatorPostProcessors) {
            aviatorPostProcessor.beforeExecute(context);
        }
    }

    public void afterProcessAllByOrder(ExecuteContext  context){
        for (AviatorPostProcessor aviatorPostProcessor : aviatorPostProcessors) {
            aviatorPostProcessor.afterExecute(context);
        }
    }

    public void addAviatorPostProcessor(AviatorPostProcessor aviatorPostProcessor){
        aviatorPostProcessors.add(aviatorPostProcessor);
    }

    public Set<AviatorPostProcessor> getAviatorPostProcessors(){
        return Collections.unmodifiableSet(aviatorPostProcessors);
    }
}
